import { _goodsCate, _goodsList } from "../../../service/api";
Page({
  data: {
    id: null,
    page: 1,
    size: 20,
    currentCategory: {},
    navList: [],
    goodsList: [],
  },
  onLoad: function (options) {
    this.setData({ id: options.id });
    this.getCateInfo();
  },
  // 获取分类信息
  async getCateInfo() {
    wx.showLoading({ title: "加载中..." });
    const res = await _goodsCate(this.data.id);
    this.setData({ currentCategory: res.goodsInfo, navList: res.cateList });
    this.getGoodsList();
  },
  // 获取商品列表
  async getGoodsList() {
    const { id, page, size } = this.data;
    const res = await _goodsList({ categoryId: id, page, size });
    this.setData({ goodsList: res.data });
    wx.hideLoading();
  },
  // 选择分类
  handleSwitchCate(e) {
    const { id } = e.currentTarget.dataset;
    if (id == this.data.id) return false;
    this.setData({ id });
    this.getCateInfo();
  },
});
